package com.szlanyou.protocol_service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @author 陈章
 * create at 2021/1/18 14:20
 * desc:
 */
public class CodeTool {
    private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};


    /**
     * 将字符串编码成16进制数字,适用于所有字符（包括中文）
     */
    public static String StringToHexString(String str) {
        StringBuilder strbd = new StringBuilder();
        byte[] bytes = null;
        try {
            bytes = str.getBytes("GBK");
            for (int i = 0; i < bytes.length; i++){
                strbd.append(Integer.toHexString(bytes[i] & 0xff));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return strbd.toString().toUpperCase();
    }

    /**
     * 将16进制数字编码成字符串,适用于所有字符（包括中文）
     */
    public static String HexStringToString(String str) {
        StringBuffer sb = new StringBuffer();
        String string = "";
        for (int i = 0; i < str.length(); i = i + 2) {
            try {
                sb.append("%").append(str.subSequence(i, i + 2));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            string = URLDecoder.decode(sb.toString(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return string;
    }

    public static String BytesToHexString(byte[] inHex) {
        int i;
        StringBuilder strResult = new StringBuilder();
        strResult.append("");
        for (i = 0; i < inHex.length; i++) {
            strResult.append(String
                    .valueOf(DIGITS_UPPER[(0xF0 & inHex[i]) >>> 4]));
            strResult.append(String.valueOf(DIGITS_UPPER[inHex[i] & 0x0F]));
        }
        return strResult.toString();
    }

    /**
     * 将字节数组转换为十六进制字符数组
     * @return 十六进制char[]
     */
    public static String BytesToHexString(byte[] inHex, int nOff, int nLen) {
        int i;
        StringBuilder strResult = new StringBuilder();
        strResult.append("");
        for (i = 0; i < nLen; i++) {
            strResult.append(String
                    .valueOf(DIGITS_UPPER[(0xF0 & inHex[nOff + i]) >>> 4]));
            strResult.append(String
                    .valueOf(DIGITS_UPPER[inHex[nOff + i] & 0x0F]));
        }
        return strResult.toString();
    }


    /**
     * 十六进制字符串到byte数组
     */
    public static byte[] HexToByteArr(String str) {
        String[] strArr = StringToStringArray(str);
        byte[] b = new byte[strArr.length];
        for (int i = 0; i < strArr.length; i++) {
            b[i] = (byte) Integer.parseInt(strArr[i], 16);
        }
        return b;
    }

    /**
     * 字符串变字符数组方法
     *
     * @param str 需要变成字符数组的字符串
     * @return 返回为两个字母为一个字符的数组
     */
    public static String[] StringToStringArray(String str) {
        if (str == null || str.length() % 2 != 0) {
            return new String[0];
        }
        int len = str.length() / 2;
        String[] strArr = new String[len];
        for (int i = 0; i < len; i++) {
            try {
                strArr[i] = str.substring(i * 2, i * 2 + 2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return strArr;
    }

}
